package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.domain.Department;
import cn.wolfcode.rbac.mapper.DepartmentMapper;
import cn.wolfcode.rbac.query.PageResult;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IDpartementService;
import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class DepartmentServiceImpl implements IDpartementService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void saveOrUpdate(Department paramDepartment) {
        if(paramDepartment.getId() == null){
            //进行插入操作
            this.departmentMapper.insert(paramDepartment);
        }else{
            //进行修改操作
            this.departmentMapper.updateByprimaryKey(paramDepartment);
        }
    }

    @Override
    public void delete(Long paramLong) {
        this.departmentMapper.deleteByPrimaryKey(paramLong);
    }

    @Override
    @Transactional(readOnly = true)
    public Department get(Long paramLong) {
        return this.departmentMapper.selectByprimaryKey(paramLong);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Department> list() {
        return this.departmentMapper.selectAll();
    }
    @Override
    @Transactional(readOnly = true)
    public PageInfo query(QueryObject qo) {
//        Integer rows = Integer.valueOf(this.departmentMapper.selectForCount(paramQueryObject));
//        if(rows.intValue() == 0){
//            return new PageResult(Integer.valueOf(paramQueryObject.getPageSize()));
//        }
//        List<Department> list = this.departmentMapper.selectForList(paramQueryObject);
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Department> list = this.departmentMapper.selectForList(qo);
        return new PageInfo(list);
    }
}

